<?php
class Controller_Services_Ebayfeedback extends Controller_Abstract
{
	function actionGetFeedback()
	{
		$this->_view['title'] = '获取评价';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		if ($this->_context->isPOST())
		{
			if (isset($_POST['shop_ids']))
			{
				$shop_ids = $_POST['shop_ids'];
				$mapping = Shop_User_Mapping::checkMapping($shop_ids);
				if ($mapping['ack'] == SUCCESS)
				{
					$result = Services_Ebay_Feedback::getFeedback($shop_ids);
					if ($result['ack'] == SUCCESS)
					{
						return $this->_redirectMessage('更新成功', '正在返回列表..', url('Services::EbayFeedback/List'), $result['ack']);
					}
					else
					{
						return $this->_redirectMessage('更新失败', $result['message'] . ', 正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
					}
				}
				else
				{
					return $this->_redirectMessage('更新失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('更新失败', '请选择店铺, 正在返回', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '列表';
		$this->_view['shipping_method'] = Shipping_Method::getTreeShippingMethod();
		$this->_view['country'] = Country::find()->order('en_name')->asArray()->getAll();
		$ps = isset($_GET['ps']) ? $_GET['ps'] : '10';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : '1';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['shop_user_mapping'] = '1';
		$result = Services_Ebay_Feedback::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Services::EbayFeedback/List', $condition));
			$this->_view['page'] = $page->getPage();
			$this->_view['feedback'] = $result['data'];
			$this->_view['url_json'] = json_encode($condition);
		}
	}
	
	function actionAddNotes()
	{
		$this->_view['title'] = '添加备注';
		if ($this->_context->isPOST())
		{
			$feedback = services_ebay_Feedback::find('id=?', $_POST['id'])->asArray()->getOne();
			$mapping = Shop_User_Mapping::checkMapping($feedback['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				QDB::getConn()->execute('update services_ebay_feedback set notes=\'' . $_POST['notes'] . '\' where id=\'' . $feedback['id'] . '\'');
				return $this->_redirectMessage('添加成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('添加失败', $mapping['message'], url('Services::EbayFeedback/List'), SUCCESS);
			}
		}
	}
	
	function actionProductNumberEditSave()
	{
		$this->_view['title'] = '产品编号修改（保存）';
		if ( ! empty($_REQUEST['id']))
		{
			if ($_REQUEST['product_number'])
			{
				$product = Product_Basic::find('number=?', $_REQUEST['product_number'])->asArray()->getOne();
			}
			elseif ($_REQUEST['product_number'] == '')
			{
				$product = array('id' => 0, 'number' => '', 'name' => '');
			}
			if (isset($product) && $product)
			{
				QDB::getConn()->execute('update services_ebay_feedback set product_basic_id=\'' . $product['id'] . '\' where id=' . $_REQUEST['id']);
				echo json_encode(array('ack' => SUCCESS, 'product_number' => $product['number'], 'product_name' => $product['name']));
				exit;
			}
			else
			{
				echo json_encode(array('ack' => FAILURE, 'product_number' => '', 'product_name' => ''));
				exit;
			}
		}
		else
		{
			echo json_encode(array('ack' => FAILURE, 'product_number' => '', 'product_name' => ''));
			exit;
		}
	}
	
	function actionExportFeedbackInfo()
	{
		$this->_view['title'] = '导出评价信息';
		if ( ! empty($_GET['ids']) || ! empty($_GET['url_json']))
		{
			if (isset($_GET['ids']))
			{
				$condition['ids'] = explode(',', $_GET['ids']);
			}
			else
			{
				$condition = json_decode($_GET['url_json'], 1);
			}
			$result = Services_Ebay_Feedback::search($condition, 0, 0);
			include Q::ini('custom_system/lib_dir') . 'PHPExcel.php';
			$phpexcel = new PHPExcel();
			$phpexcel->getProperties()->setCreator(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setLastModifiedBy(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setTitle('ebay评价信息表');
			$phpexcel->getProperties()->setSubject('ebay评价信息表');
			$phpexcel->getProperties()->setDescription('ebay评价信息表-运营系统支撑系统');
			$phpexcel->getProperties()->setKeywords('ebay评价信息表');
			$phpexcel->getProperties()->setCategory('ebay评价信息表');
			$phpexcel->getActiveSheetIndex(0);
			$phpexcel->getActiveSheet()->setTitle('ebay评价信息表');
			$phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
			$phpexcel->getActiveSheet()->SetCellValue('A1', '顾客名称');
			$phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth('10');
			$phpexcel->getActiveSheet()->SetCellValue('B1', '评价类型');
			$phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
			$phpexcel->getActiveSheet()->SetCellValue('C1', '店铺');
			$phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth('15');
			$phpexcel->getActiveSheet()->SetCellValue('D1', 'ItemID');
			$phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth('10');
			$phpexcel->getActiveSheet()->SetCellValue('E1', '产品编号');
			$phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth('20');
			$phpexcel->getActiveSheet()->SetCellValue('F1', '产品名称');
			$phpexcel->getActiveSheet()->SetCellValue('G1', '单价');
			$phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth('18');
			$phpexcel->getActiveSheet()->SetCellValue('H1', '发货时间');
			$phpexcel->getActiveSheet()->getColumnDimension('I')->setWidth('18');
			$phpexcel->getActiveSheet()->SetCellValue('I1', '评价时间');
			$phpexcel->getActiveSheet()->SetCellValue('J1', '间隔(天)');
			$phpexcel->getActiveSheet()->getColumnDimension('K')->setWidth('15');
			$phpexcel->getActiveSheet()->SetCellValue('K1', '国家');
			$phpexcel->getActiveSheet()->getColumnDimension('L')->setWidth('12');
			$phpexcel->getActiveSheet()->SetCellValue('L1', '邮寄');
			$i = 2;
			foreach ($result['data'] as $r)
			{
				$phpexcel->getActiveSheet()->SetCellValue('A' . $i, $r['user']);
				$phpexcel->getActiveSheet()->SetCellValue('B' . $i, Helper_BSS_Normal::getIniOne('services_ebay_feedback_type', 'value', $r['type'], 'name'));
				$phpexcel->getActiveSheet()->SetCellValue('C' . $i, $r['shop_name']);
				$phpexcel->getActiveSheet()->getCell('D' . $i)->setValueExplicit($r['shop_item_id'], PHPExcel_Cell_DataType::TYPE_STRING);
				$phpexcel->getActiveSheet()->getCell('E' . $i)->setValueExplicit($r['product_number'], PHPExcel_Cell_DataType::TYPE_STRING);
				$phpexcel->getActiveSheet()->SetCellValue('F' . $i, $r['product_name']);
				$phpexcel->getActiveSheet()->SetCellValue('G' . $i, $r['currency_symbol'] . sprintf('%.2f', $r['item_price']));
				$phpexcel->getActiveSheet()->SetCellValue('H' . $i, $r['shipped_time']);
				$phpexcel->getActiveSheet()->SetCellValue('I' . $i, $r['create_time']);
				$phpexcel->getActiveSheet()->SetCellValue('J' . $i, $r['shipped_time'] ? ceil(((strtotime($r['create_time']) - strtotime($r['shipped_time'])) / (3600 * 24))) : '');
				$phpexcel->getActiveSheet()->SetCellValue('K' . $i, $r['country']);
				$phpexcel->getActiveSheet()->SetCellValue('L' . $i, $r['shipping_method_name']);
				$i++;
			}
			$outputFileName = 'ebay评价信息表.xls';
			$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
			header("Content-Type: application/vnd.ms-excel");
			header("Content-Type: application/octet-stream");
			header("Content-Type: applicaiton/download");
			header('Content-Disposition:inine;filename="' . $outputFileName . '"');
			header("Content-Transfer-Encoding: binary");
			header("Pragma: no-cache");
			$objWriter->save('php://output');
			exit;
		}
	}
}